local app = require 'app'
local game = app.game.running
local util = game.require 'util.util'
local sound_effect = {}

local function get_sex(player)
    if player.gender == 1 then
        return "男"
    else
        return "女"
    end
end

local type2primetype = 
{
    solo = "单",
    solo_chain_5 = "顺子",
    solo_chain_6 = "顺子",
    solo_chain_7 = "顺子",
    solo_chain_8 = "顺子",
    solo_chain_9 = "顺子",
    solo_chain_10 = "顺子",
    solo_chain_11 = "顺子",
    solo_chain_12 = "顺子",
    pair = "对",
    pair_chain_3 = "连对",
    pair_chain_4 = "连对",
    pair_chain_5 = "连对",
    pair_chain_6 = "连对",
    pair_chain_7 = "连对",
    pair_chain_8 = "连对",
    pair_chain_9 = "连对",
    pair_chain_10 = "连对",
    trio = "三",
    trio_chain_2 = "三顺",
    trio_chain_3 = "三顺",
    trio_chain_4 = "三顺",
    trio_chain_5 = "三顺",
    trio_chain_6 = "三顺",
    trio_solo = "三带一",
    trio_solo_chain_2 = "飞机",
    trio_solo_chain_3 = "飞机",
    trio_solo_chain_4 = "飞机",
    trio_solo_chain_5 = "飞机",
    trio_pair = "三带二",
    trio_pair_chain_2 = "飞机",
    trio_pair_chain_3 = "飞机",
    trio_pair_chain_4 = "飞机",
    four_two_solo = "四带一对",
    four_two_pair = "四带二对",
    bomb = "炸弹",
    rocket = "火箭",
}

function sound_effect.player_handout(player, card_ids)
    if not card_ids then
        return
    end

    if #card_ids == 0 then
        game.audio.post(string.format('doudizhu:%s不管%s', get_sex(player), math.random(1, 4)))
        return
    end

    if #card_ids <= 2 then
        game.audio.post('doudizhu:摔牌1')
    elseif #card_ids <= 5 then
        game.audio.post('doudizhu:摔牌2')
    else
        game.audio.post('doudizhu:摔牌3')
    end

    local action = util.card_ids2action(card_ids)
    local type = util.action2type(action)
    local primetype = type2primetype[type]
    if primetype == "单" then
        game.audio.post(string.format('doudizhu:%s单%s', get_sex(player), action))
    elseif primetype == "对" then
        game.audio.post(string.format('doudizhu:%s对%s', get_sex(player), string.sub(action, 1, 1)))
    elseif primetype == "三" then
        game.audio.post(string.format('doudizhu:%s三%s', get_sex(player), string.sub(action, 1, 1)))
    elseif primetype == "顺子" then
        if string.find(action, "A") then
            game.audio.post(string.format('doudizhu:%s通天顺', get_sex(player)))
        else
            game.audio.post(string.format('doudizhu:%s顺子', get_sex(player)))
        end
    elseif primetype == "连对" then
        game.audio.post(string.format('doudizhu:%s连对', get_sex(player)))
    elseif primetype == "三顺" then
        game.audio.post(string.format('doudizhu:%s三顺', get_sex(player)))
    elseif primetype == "三带一" then
        game.audio.post(string.format('doudizhu:%s三带一', get_sex(player)))
    elseif primetype == "三带二" then
        game.audio.post(string.format('doudizhu:%s三带二', get_sex(player)))
    elseif primetype == "飞机" then
        game.audio.post(string.format('doudizhu:%s飞机%s', get_sex(player), math.random(1, 4)))
    elseif primetype == "四带一对" then
        game.audio.post(string.format('doudizhu:%s四带一对', get_sex(player)))
    elseif primetype == "四带二对" then
        game.audio.post(string.format('doudizhu:%s四带二对', get_sex(player)))
    elseif primetype == "炸弹" then
        game.audio.post(string.format('doudizhu:%s炸弹%s', get_sex(player), math.random(1, 3)))
    elseif primetype == "火箭" then
        game.audio.post(string.format('doudizhu:%s火箭%s', get_sex(player), math.random(1, 2)))
    end
end

function sound_effect.bid_result(player, action_id)
    if action_id == game.util.consts.bid_pass.id then
        game.audio.post(string.format("doudizhu:%s不叫", get_sex(player)))
    elseif action_id == game.util.consts.bid_1.id then
        game.audio.post(string.format("doudizhu:%s叫地主", get_sex(player)))
    elseif action_id == game.util.consts.bid_2.id then
        game.audio.post(string.format("doudizhu:%s抢地主1", get_sex(player)))
    elseif action_id == game.util.consts.bid_3.id then
        game.audio.post(string.format("doudizhu:%s抢地主2", get_sex(player)))
    end
    
end

return sound_effect